package com.topisv.tms.utils.http;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.topisv.tms.entity.BackupTask;
import com.topisv.tms.entity.EdiOrder;
//import com.topisv.tms.jms.SendMessageProducer;
import com.topisv.tms.jms.vo.SendMessage;
import com.topisv.tms.jms.vo.SendOutMessage;
import com.topisv.tms.rest.entity.Batch;
import com.topisv.tms.service.BackupTaskService;
import com.topisv.tms.service.EdiOrderService;
import com.topisv.tms.service.SendOutOrdenoHT;
import com.topisv.tms.service.deppon.DepponService;
import com.topisv.tms.service.shipped.ShippedBatchService;
import com.topisv.tms.service.work.WorkBatchService;
import com.topisv.tms.service.yto.YTOCreateOrdenoService;
import com.topisv.tms.utils.enums.JMSMessageType;
import com.topisv.tms.utils.enums.StatusEnums;

import net.sf.json.JSONObject;

/**
 * 发送消息
 * @author Administrator
 *
 */
@Component
public class SynchroSendMessage implements Runnable {
	private static final Logger log = LoggerFactory.getLogger(SynchroSendMessage.class);
	
//	@Autowired
//	private SendMessageProducer sendMessageProducer;
	@Autowired
	private BackupTaskService backupTaskService;
	@Autowired
	private ShippedBatchService shippedBatchService;
	@Autowired
	private YTOCreateOrdenoService yTOCreateOrdenoService;
	@Autowired
	private SendOutOrdenoHT sendOutOrdenoHT;
	@Autowired
	private EdiOrderService ediOrderService;
	@Autowired
	private WorkBatchService workBatchService;
	@Autowired
	private DepponService depponService;
	@Override
	public void run() {
		while(true){
			try {
				String message=SynchroShippedBatchQueue.takeSendMessageQueue();
//				log.info("发送消息:"+message);
//				String[] lists=message.split(StatusEnums.split.getStatus());
//				StringBuffer sb=new StringBuffer();
//				SendMessage sendMessage=null;
//				if("createBatch".equals(lists[0])){
//					log.info("推送出库批次:"+lists[1]);
//					BackupTask task=this.backupTaskService.findByBackupTaskCode(lists[1]);
//					sb.append(lists[0]+StatusEnums.split.getStatus());
//					Batch batch = shippedBatchService.getBatch(task.getBatchCode(), task.getCustomerCode());
//					JSONObject json = JSONObject.fromObject(batch);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(json.toString());
//					sendMessageProducer.sendTopic(sendMessage);
//				}
//				else if("createBatchWMS".equals(lists[0]))
//				{
//					log.info("wms推送出库批次:"+lists[1]);
//					try {
//						Thread.sleep(5000);
//					} catch (Exception e) {
//						// TODO Auto-generated catch block
//						e.printStackTrace();
//					}
//					String json = workBatchService.sendBatch(lists[1]);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(json.toString());
//					sendMessageProducer.sendTopic(sendMessage);
//				}
//				else if("kuaidi_sendout".equals(lists[0]))
//				{
//					log.info("调用快递100接口:"+lists[1]);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}
//				else if("createCustomers".equals(lists[0])){
//					log.info("创建商家:"+lists[1]);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}else if("createOrdenoYTO".equals(lists[0])){
//					log.info("向圆通电子面单创建订单:"+lists[1]);
//					sendMessage=yTOCreateOrdenoService.createYTOOrdeno(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}else if(JMSMessageType.CREATE_DEPPON_ORDER.getType().equals(lists[0])){
//					log.info("向德邦物流平台创建订单:"+lists[1]);
//					sendMessage = depponService.createDepponOrder(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}else if("sendOutOrdeno".equals(lists[0])){
//					log.info("订单调用ERP接口发货:"+lists[1]);
//					EdiOrder e=ediOrderService.findByOrdeno(lists[1]);
//					if(e!=null&&(!"cancel".equals(e.getOrderStatus()))){
//						ObjectMapper objectMapper = new ObjectMapper();
//						/*if("oms".equals(e.getErpType()) && !"HHQZ".equals(e.getCustomerCode())){//OMS发货方式
//							WorkOrder workOrder=this.workOrderService.loadByOrdernoOrCustomerorderno(e.getOrdeno());
//							List<EdiOrderItem> eis=this.ediOrderItemService.findByOrdeno(e.getOrdeno());
//							e.setSkulst(eis);
//							if("ebill".equals(workOrder.getBillType())){
//								e.setMailNo(workOrder.getEbillNo());
//							}else{
//								e.setMailNo(workOrder.getExpressNo());
//							}
//							e.setCacode(workOrder.getExpressCompany());
//							String m=objectMapper.writeValueAsString(e);
//							sendMessage=new SendMessage();
//							sendMessage.setType("sendOutOMSOrdeno");
//							sendMessage.setMessage(m);
//							sendMessageProducer.sendTopic(sendMessage);
//						}else{//http的发货方式
//*/							List<SendOutMessage> sendOutMessages=sendOutOrdenoHT.getSendOutMessage(lists[1]);
//							if(sendOutMessages!=null){
//								for(SendOutMessage sm:sendOutMessages){
//									String m=objectMapper.writeValueAsString(sm);
//									sendMessage=new SendMessage();
//									sendMessage.setType(lists[0]);
//									sendMessage.setMessage(m);
//									sendMessageProducer.sendTopic(sendMessage);
//								}
//							}
//						//}
//						sendMessage=new SendMessage();
//						sendMessage.setType("sendOutOrdeno_mid");
//						sendMessage.setMessage(lists[1]);
//						sendMessageProducer.sendTopic(sendMessage);
//					}
//				}else if("cancelOrdenoYTO".equals(lists[0])){
//					log.info("向圆通电子面单平台取消订单:"+lists[1]);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}else if("cancelOrdenoYUNDA".equals(lists[0])){
//					log.info("向韵达电子面单平台取消订单:"+lists[1]);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}
//				else if("warehouseInfo".equals(lists[0])){
//					log.info("仓内作业状态回传:"+lists[1]);
//					sendMessage=new SendMessage();
//					sendMessage.setType(lists[0]);
//					sendMessage.setMessage(lists[1]);
//					sendMessageProducer.sendTopic(sendMessage);
//				}else if("cancelOrdenoZTO".equals(lists[0])){
//                    log.info("向中通电子面单平台取消订单:"+lists[1]);
//                    sendMessage=new SendMessage();
//                    sendMessage.setType(lists[0]);
//                    sendMessage.setMessage(lists[1]);
//                    sendMessageProducer.sendTopic(sendMessage);
//                }
				
			} catch (Exception e) {
				log.error("error",e);
			}
		}

	}

}
